import { Component, Injector, OnInit, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/app-component-base';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { CategoryAddComponent } from '../category-add/category-add.component';
import { ProductCategoryServiceProxy,
  ListResultDtoOfCategoryOutputDto,
  CategoryOutputDto,
  ProductType } from '@shared/service-proxies/service-proxies';

@Component({
    selector: 'category-list',
    templateUrl: 'category-list.component.html',
    styleUrls: ['category-list.component.scss'],
    animations: [appModuleAnimation()],
    providers: [ProductCategoryServiceProxy]
})
export class CategoryListComponent extends AppComponentBase implements OnInit {
    @ViewChild('createCategoryModal') createCategoryModal: CategoryAddComponent;
    categories: ListResultDtoOfCategoryOutputDto = new ListResultDtoOfCategoryOutputDto();
    condition: GetCategoriesCondition = new GetCategoriesCondition();
    constructor(
        injector: Injector,
        private _categoryService: ProductCategoryServiceProxy
    ) {
        super(injector);
    }

    ngOnInit(): void {
        this.getCategories();
    }

    getCategories(): void {
        this._categoryService.getCategoriesAsync(ProductType.Jewelry,this.condition.parentFullCode, this.condition.parentId)
            .subscribe((result: ListResultDtoOfCategoryOutputDto) => {
                this.categories = result;
            });
    }

    deleteCategory(id: number): void {
        this._categoryService.deleteAsync(id).subscribe(() => {
            abp.notify.success('分类删除成功!');
            this.getCategories();
        });
    }

    createCategory(): void {
        this.createCategoryModal.show();
    }
}


class GetCategoriesCondition {
    parentFullCode: string;
    parentId: number;
}
